
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>03. 接口 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="04.class.html" />
    
    
    <link rel="prev" href="02. basic-types.md" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="./">
            
                <a href="./">
            
                    
                    00. 简介
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="01.typescript-in-5-minutes.html">
            
                <a href="01.typescript-in-5-minutes.html">
            
                    
                    01. 5 分钟上手
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="02. basic-types.md">
            
                <span>
            
                    
                    02. 基础类型
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.4" data-path="03.interface.html">
            
                <a href="03.interface.html">
            
                    
                    03. 接口
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="04.class.html">
            
                <a href="04.class.html">
            
                    
                    04. 类
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="05.function.html">
            
                <a href="05.function.html">
            
                    
                    05. 函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="06.generics.html">
            
                <a href="06.generics.html">
            
                    
                    06. 泛型
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="." >03. 接口</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <p>&#x5B9A;&#x4E49;&#x65B0;&#x7684;&#x6570;&#x636E;&#x7ED3;&#x6784;&#x7C7B;&#x578B;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">interface</span> LabelledValue {
  label: <span class="hljs-built_in">string</span>;
  width: <span class="hljs-built_in">number</span>;  &#x53EF;&#x9009;&#x7C7B;&#x578B;
  [_:<span class="hljs-built_in">string</span>]: <span class="hljs-built_in">any</span>;  &#x672A;&#x77E5;&#x5C5E;&#x6027; &#x672A;&#x77E5;&#x7C7B;&#x578B;&#xFF08;&#x5B98;&#x65B9;&#x53EB;&#x505A;&#x7D22;&#x5F15;&#x7B7E;&#x540D;&#xFF09;
  readonly y: <span class="hljs-built_in">number</span>;  &#x53EA;&#x8BFB;&#x5C5E;&#x6027;&#xFF0C;&#x53EA;&#x80FD;&#x8D4B;&#x503C;&#x4E00;&#x6B21;
}

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">printLabel</span>(<span class="hljs-params">labelledObj LabelledValue</span>) </span>{
  <span class="hljs-built_in">console</span>.log(labelledObj.label);
}
</code></pre>
<h2 id="&#x53EA;&#x8BFB;&#x5C5E;&#x6027;">&#x53EA;&#x8BFB;&#x5C5E;&#x6027;</h2>
<p><code>interface</code> &#x53EF;&#x4EE5;&#x5B9A;&#x4E49;&#x53EA;&#x8BFB;&#x5C5E;&#x6027;&#xFF08;<code>readonly</code>&#xFF09;&#x3002;</p>
<p>&#x540C;&#x65F6;&#xFF0C;<code>typescript</code> &#x4E5F;&#x63D0;&#x4F9B;&#x4E86;&#x4E00;&#x79CD;&#x65B0;&#x7684;&#x6570;&#x636E;&#x7C7B;&#x578B;&#xFF1A;<code>ReadonlyArrayT</code>&#xFF0C;&#x6B64;&#x7C7B;&#x578B;&#x7684;&#x6570;&#x7EC4;&#x5C06;&#x5728;&#x5B9A;&#x4E49;&#x540E;&#x4E0D;&#x80FD;&#x6DFB;&#x52A0;&#x3001;&#x4FEE;&#x6539;&#x6570;&#x7EC4;&#x5143;&#x7D20;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">let</span> a <span class="hljs-built_in">number</span>[] = [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>];
<span class="hljs-keyword">let</span> ro ReadonlyArraynumber = a;
ro[<span class="hljs-number">0</span>] = <span class="hljs-number">12</span>; <span class="hljs-comment">// error!</span>
ro.push(<span class="hljs-number">5</span>); <span class="hljs-comment">// error!</span>
ro.length = <span class="hljs-number">100</span>; <span class="hljs-comment">// error!</span>
a = ro; <span class="hljs-comment">// error!</span>
</code></pre>
<h2 id="&#x51FD;&#x6570;&#x7C7B;&#x578B;">&#x51FD;&#x6570;&#x7C7B;&#x578B;</h2>
<p><code>interface</code> &#x9664;&#x4E86;&#x5B9A;&#x4E49;&#x666E;&#x901A;&#x5BF9;&#x8C61;&#x5916;&#xFF0C;&#x8FD8;&#x80FD;&#x5B9A;&#x4E49;&#x51FD;&#x6570;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">interface</span> SearchFunc {
  (source: <span class="hljs-built_in">string</span>, subString: <span class="hljs-built_in">string</span>) <span class="hljs-built_in">boolean</span>;
}

<span class="hljs-keyword">let</span> mySearch SearchFunc;
mySearch = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">source <span class="hljs-built_in">string</span>, subString <span class="hljs-built_in">string</span></span>) </span>{
  <span class="hljs-keyword">let</span> result = source.search(subString);
  <span class="hljs-keyword">return</span> result &gt; <span class="hljs-number">-1</span>;
}
</code></pre>
<h3 id="&#x6CE8;&#x610F;">&#x6CE8;&#x610F;</h3>
<p>&#x8FD9;&#x91CC;&#x5B9A;&#x4E49;&#x7684;&#x51FD;&#x6570;&#xFF0C;&#x53C2;&#x6570;&#x53EF;&#x4EE5;&#x5C11;&#xFF0C;&#x4F46;&#x4E0D;&#x80FD;&#x591A;&#x3002;</p>
<p>&#x5982;&#x679C;&#x6211;&#x4EEC;&#x7684;&#x51FD;&#x6570;&#x662F;&#x4E0D;&#x5B9A;&#x4E2A;&#x6570;&#x53C2;&#x6570;&#x7684;&#x600E;&#x4E48;&#x529E;&#xFF1F;typescript &#x63D0;&#x4F9B;&#x4E86; ES6 spread &#x65B9;&#x6848;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">interface</span> Add {
  (str: <span class="hljs-built_in">string</span>, ...args: <span class="hljs-built_in">any</span>[]): <span class="hljs-built_in">number</span>;
}

<span class="hljs-keyword">let</span> add Add;
add = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">str, a, b</span>) </span>{
  <span class="hljs-built_in">console</span>.log(str)
  <span class="hljs-built_in">console</span>.log(a, b);
}
</code></pre>
<h2 id="&#x7D22;&#x5F15;&#x7B7E;&#x540D;">&#x7D22;&#x5F15;&#x7B7E;&#x540D;</h2>
<p>&#x5728;&#x4E00;&#x5F00;&#x59CB;&#x7684;&#x4F8B;&#x5B50;&#x91CC;&#x6709;&#x7D22;&#x5F15;&#x7B7E;&#x540D;&#x7684;&#x4F8B;&#x5B50;&#xFF1A;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">interface</span> LabelledValue {
  [_:<span class="hljs-built_in">string</span>]: <span class="hljs-built_in">any</span>;  &#x672A;&#x77E5;&#x5C5E;&#x6027; &#x672A;&#x77E5;&#x7C7B;&#x578B;&#xFF08;&#x5B98;&#x65B9;&#x53EB;&#x505A;&#x7D22;&#x5F15;&#x7B7E;&#x540D;&#xFF09;
}
</code></pre>
<h3 id="&#x6CE8;&#x610F;">&#x6CE8;&#x610F;</h3>
<ol>
<li><p>&#x8FD9;&#x91CC;&#x7684; <code>_</code> &#x53EA;&#x662F;&#x4F5C;&#x5360;&#x4F4D;&#x7528;&#xFF0C;&#x5E76;&#x4E0D;&#x8868;&#x793A;&#x5177;&#x4F53;&#x7684;&#x5C5E;&#x6027;&#x540D;&#xFF0C;&#x56E0;&#x6B64;&#x53EF;&#x4EE5;&#x586B;&#x201C;&#x4EFB;&#x610F;&#x201D;&#x5185;&#x5BB9;&#x3002;</p>
</li>
<li><p>&#x7D22;&#x5F15;&#x7B7E;&#x540D;&#x652F;&#x6301; string &#x548C; number &#x4E24;&#x79CD;&#x683C;&#x5F0F;</p>
</li>
<li><p>&#x5982;&#x679C;&#x5C06; any &#x6539;&#x6210;&#x6307;&#x5B9A;&#x683C;&#x5F0F;&#xFF0C;&#x5219;&#x663E;&#x793A;&#x5B9A;&#x4E49;&#x7684;&#x5C5E;&#x6027;&#x4E5F;&#x9700;&#x8981;&#x662F;&#x8BE5;&#x6307;&#x5B9A;&#x5C5E;&#x6027;&#xFF0C;&#x5982;&#x4E0B;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">interface</span> LabelledValue {
  [_:<span class="hljs-built_in">string</span>]: <span class="hljs-built_in">string</span>;  &#x672A;&#x77E5;&#x5C5E;&#x6027; &#x672A;&#x77E5;&#x7C7B;&#x578B;&#xFF08;&#x5B98;&#x65B9;&#x53EB;&#x505A;&#x7D22;&#x5F15;&#x7B7E;&#x540D;&#xFF09;
  a: <span class="hljs-built_in">string</span>; <span class="hljs-comment">// correct</span>
    b: <span class="hljs-built_in">number</span>; <span class="hljs-comment">// error</span>
}
</code></pre>
</li>
</ol>
<h2 id="&#x7C7B;&#x7C7B;&#x578B;">&#x7C7B;&#x7C7B;&#x578B;</h2>
<p>&#x63A5;&#x53E3;&#x4E5F;&#x53EF;&#x4EE5;&#x7528;&#x4E8E;&#x5B9A;&#x4E49;&#x4E00;&#x4E2A;&#x7C7B;&#x7684;&#x516C;&#x6709;&#x5C5E;&#x6027;&#x548C;&#x65B9;&#x6CD5;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">interface</span> ClockInterface {
    currentTime: <span class="hljs-built_in">Date</span>;
    setTime(d: <span class="hljs-built_in">Date</span>);
}

<span class="hljs-keyword">class</span> Clock <span class="hljs-keyword">implements</span> ClockInterface {
    currentTime: <span class="hljs-built_in">Date</span>;
    setTime(d: <span class="hljs-built_in">Date</span>) {
        <span class="hljs-keyword">this</span>.currentTime = d;
    }
    <span class="hljs-keyword">constructor</span>(h: number, m: number) { }
}
</code></pre>
<h2 id="&#x7EE7;&#x627F;&#x63A5;&#x53E3;">&#x7EE7;&#x627F;&#x63A5;&#x53E3;</h2>
<pre><code class="lang-ts"><span class="hljs-keyword">interface</span> Shape {
    color: <span class="hljs-built_in">string</span>;
}

<span class="hljs-keyword">interface</span> PenStroke {
    penWidth: <span class="hljs-built_in">number</span>;
}

<span class="hljs-keyword">interface</span> Square <span class="hljs-keyword">extends</span> Shape, PenStroke {
    sideLength: <span class="hljs-built_in">number</span>;
}

<span class="hljs-keyword">let</span> square = &lt;Square&gt;{};
square.color = <span class="hljs-string">&quot;blue&quot;</span>;
square.sideLength = <span class="hljs-number">10</span>;
square.penWidth = <span class="hljs-number">5.0</span>;
</code></pre>
<h2 id="&#x63A5;&#x53E3;&#x7EE7;&#x627F;&#x7C7B;">&#x63A5;&#x53E3;&#x7EE7;&#x627F;&#x7C7B;</h2>
<p>&#x76EE;&#x524D;&#x6CA1;&#x6709;&#x5F88;&#x7406;&#x89E3;&#xFF0C;todo</p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="02. basic-types.md" class="navigation navigation-prev " aria-label="Previous page: 02. 基础类型">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="04.class.html" class="navigation navigation-next " aria-label="Next page: 04. 类">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"03. 接口","level":"1.4","depth":1,"next":{"title":"04. 类","level":"1.5","depth":1,"path":"04.class.md","ref":"04.class.md","articles":[]},"previous":{"title":"02. 基础类型","level":"1.3","depth":1,"path":"02. basic-types.md","ref":"02. basic-types.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":["livereload"],"pluginsConfig":{"livereload":{},"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"03.interface.md","mtime":"2018-10-02T07:48:04.107Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2018-10-10T12:13:20.554Z"},"basePath":".","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="gitbook/gitbook.js"></script>
    <script src="gitbook/theme.js"></script>
    
        
        <script src="gitbook/gitbook-plugin-livereload/plugin.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

